package com.xutong.myfunction.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xutong.myfunction.R;
import com.xutong.myfunction.socket.TCPSocketClient;
import com.xutong.myfunction.socket.TCPSocketServer;
import com.xutong.myfunction.socket.UDPSocket;

public class SocketTestActivity extends AppCompatActivity {

    private static final String TAG = "SocketTestActivity";

    private TCPSocketServer tcpSocketServer = null;

    private TextView tvMsg;
    private EditText etSengMsg;
    private EditText etTargetIP;
//    private EditText etLocalIP;
    private EditText etTargetPort;
    private EditText etLocalPort;

    private Button btnUDPReceive;
    private Button btnTCPReceive;

    private UDPSocket udpSocket = null;
    private SocketThread socketThread = null;
    private static boolean isBind = false;

    private EditText etTCPMsg;
    private TextView tvTCPMsg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket_test);

        tvMsg = (TextView) findViewById(R.id.tvMsg);
        etSengMsg = (EditText) findViewById(R.id.etSendMsg);
        etTargetIP = (EditText) findViewById(R.id.etTargetIP);
//        etLocalIP = (EditText) findViewById(R.id.etLocalIP);
        etTargetPort = (EditText) findViewById(R.id.etTargetPort);
        etLocalPort = (EditText) findViewById(R.id.etLocalPort);

        btnUDPReceive = (Button) findViewById(R.id.btnUDPReceive);

        btnTCPReceive = (Button) findViewById(R.id.btnTCPReceive);
        tvTCPMsg = (TextView) findViewById(R.id.tvTCPMsg);
        etTCPMsg = (EditText) findViewById(R.id.etTCPMsg);

//        etLocalIP.setText("192.168.107.102");
        etTargetIP.setText("192.168.107.103");
        etLocalPort.setText("5000");
        etTargetPort.setText("6000");

    }

    public void IntentOnClick(View v) {

        switch (v.getId()) {
            case R.id.btnTCPSend:
                String sendTCPMsg = etTCPMsg.getText().toString();
                if (TextUtils.isEmpty(sendTCPMsg)){
                    return;
                }
                TCPSendtask tcpSendtask = new TCPSendtask();
                tcpSendtask.execute(sendTCPMsg);
                break;
            case R.id.btnTCPReceive:
                // 开启服务器

				if (TCPSocketServer.run){
                    tcpSocketServer.setOnOff(false);
                    btnTCPReceive.setText("TCP服务端关闭");
                }else {
                    tcpSocketServer =   new TCPSocketServer();
                    tcpSocketServer.setOnOff(true);
                    tcpSocketServer.start();
                    btnTCPReceive.setText("TCP服务端开启");
                }

                break;
            case R.id.btnUDPSend:
                String sendMsg = etSengMsg.getText().toString();
                String ip = etTargetIP.getText().toString();
                String targetPort = etTargetPort.getText().toString();
                if (isBind){
                    if (!TextUtils.isEmpty(sendMsg)&&!TextUtils.isEmpty(ip)&&!TextUtils.isEmpty(targetPort)){
                        UDPSendtask udpSendtask = new UDPSendtask();
                        udpSendtask.execute(sendMsg,ip,targetPort);
                    }
                }else {
                    Toast.makeText(SocketTestActivity.this,"你的服务已经关闭",Toast.LENGTH_SHORT).show();
                }


                break;
            case R.id.btnUDPReceive:
                String localPort = etLocalPort.getText().toString();
                if (TextUtils.isEmpty(localPort)){
                    Toast.makeText(SocketTestActivity.this,"端口不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }

                if (isBind){
                    isBind = false;
                    if (udpSocket!=null){
                        udpSocket.colseSocket();
                    }
                    btnUDPReceive.setText("UDP服务器已关闭");
                }else {
                    udpSocket = new UDPSocket();
                    if (isBind = udpSocket.bindPort(Integer.parseInt(localPort))){
                        socketThread = new SocketThread();
                        socketThread.start();
                        btnUDPReceive.setText("UDP服务器已开启");
                    }else {
                        Log.d(TAG,"绑定失败");
                    }
                }

                break;
        }
    }

    class TCPSendtask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... params) {

            TCPSocketClient  tcpClient = new TCPSocketClient();
			return tcpClient.send(params[0]);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            tvTCPMsg.setText(s);
        }
    }


    class UDPSendtask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... params) {

           udpSocket.sendSocketData(params[1],Integer.parseInt(params[2]),params[0]);

            return "消息已发送";
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
//            tvMsg.setText(s);
        }
    }


    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            String text = String.valueOf(msg.obj);

            tvMsg.setText(text);


        }
    };



    class SocketThread extends Thread{

        @Override
        public void run() {
            while (isBind){
                byte[] data = udpSocket.receiveSocketData();
                if (data!=null&&data.length>0){
                    String getData = new String(data);
                    Message msg = new Message();
                    msg.obj = getData;
                    handler.sendMessage(msg);
                }
            }

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        isBind = false;
        if (udpSocket!=null){
            udpSocket.colseSocket();
        }
    }
}
